package itheima.zuiye.Test1;

import java.util.*;

public class LotteryBox implements Runnable {
    private static List<Integer> pool = new ArrayList<>(List.of(10, 5, 20, 50, 100, 200, 500, 800, 2, 80, 300, 700));
    private static Random random = new Random();
    private String name;
    private List<Integer> results = new ArrayList<>();
    private int maxAward = 0;
    private int totalAmount = 0;

    public LotteryBox(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        for (int i = 0; i < 6; i++) {
            int award = drawLottery();
            results.add(award);
            totalAmount += award;
            if (award > maxAward) {
                maxAward = award;
            }
            // 第一种情况：每次抽出一个奖项就打印一个（随机）
            System.out.println(name + " 又产生了一个 " + award + " 元大奖");
        }

        // 第二种和第三种情况：抽完时一次性打印（随机）
        printResults();
    }

//    private synchronized int drawLottery() {
//        if (pool.isEmpty()) {
//            return 0;
//        }
//        int index = random.nextInt(pool.size());
//        int award = pool.get(index);
//        pool.remove(index);
//        return award;
//    }
    private synchronized int drawLottery() {
        if (pool.isEmpty()) {
            return 0;
        }
        int index = random.nextInt(pool.size());
        return Optional.ofNullable(pool.get(index))
                .orElse(0); // 如果为 null 则返回默认值 0
    }


    private void printResults() {
        Collections.sort(results);
        System.out.println(name + " 总共产生了6个奖项，分别为：" + results + " 最高奖项为" + maxAward + "元，总计额为" + totalAmount + "元");
    }
}

